import {
	ref,
	onMounted
} from 'vue';

const mainDomain = "http://api.sqhyw.net:90";
const backupDomain = "http://api.nanx.com:90"; // 修正了原始代码中的一个拼写错误

function request(url, method, data) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: method,
			data,
			success(response) {
				resolve(response.data);
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				reject(err);
			}
		});
	});
}

const yezi = {
	// 登录接口
	async login(userInfo) {
		const loginUrl = `${mainDomain}/api/logins`;
		const params = {
			username: userInfo.username,
			password: userInfo.password
		};
		const response = request(loginUrl, "GET", params)
		console.log(response)
		return response
	},

	// 获取余额接口
	async getBalance(token) {
		const balanceUrl = `${mainDomain}/api/get_myinfo`;
		const params = {
			token
		};
		const response = await uni.request({
			url: balanceUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	},

	// 获取手机号接口
	async getMobile(token, projectId, special = null, phoneNum = null, scope = null, ...kwargs) {
		const mobileUrl = `${mainDomain}/api/get_mobile`;
		let params = {
			token,
			project_id: projectId,
			...kwargs
		};
		if (special) params.special = special;
		if (phoneNum) params.phone_num = phoneNum;
		if (scope) params.scope = scope;
		const response = await uni.request({
			url: mobileUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	},

	// 获取短信消息接口
	async getMessage(token, projectId, phoneNum, special = null, timeout = 60) {
		const messageUrl = `${mainDomain}/api/get_message`;
		const params = {
			token,
			project_id: projectId,
			phone_num: phoneNum
		};
		if (special) params.special = special;

		const startTime = Date.now();
		while (true) {
			const response = await uni.request({
				url: messageUrl,
				method: 'GET',
				params: params
			});
			if (response.status === 200) {
				const result = response.data;
				if (result.message === 'ok' && result.data && result.data.length > 0) {
					return result;
				} else {
					await new Promise(resolve => setTimeout(resolve, 2000));
					if (Date.now() - startTime > timeout * 1000) {
						console.log("Timeout waiting for message.");
						return null;
					}
				}
			} else {
				console.log(`Failed to get message, status code: ${response.status}`);
				return null;
			}
		}
	},

	// 释放手机号接口
	async freeMobile(token, phoneNum = null, projectId = null, special = null) {
		const freeUrl = `${mainDomain}/api/free_mobile`;
		let params = {
			token
		};
		if (phoneNum) params.phone_num = phoneNum;
		if (projectId) params.project_id = projectId;
		if (special) params.special = special;
		const response = await uni.request({
			url: freeUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	},

	// 添加黑名单接口
	async addBlacklist(token, projectId, phoneNum) {
		const blacklistUrl = `${mainDomain}/api/add_blacklist`;
		const params = {
			token,
			project_id: projectId,
			phone_num: phoneNum
		};
		const response = await uni.request({
			url: blacklistUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	},

	// 获取加入信息接口
	async getJoin(token) {
		const joinUrl = `${mainDomain}/api/get_join`;
		const params = {
			token
		};
		const response = await uni.request({
			url: joinUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	},

	// 订阅加入接口
	async subJoin(token, key_) {
		const subJoinUrl = `${mainDomain}/api/sub_join`;
		const params = {
			token,
			key_: key_
		};
		const response = await uni.request({
			url: subJoinUrl,
			method: 'GET',
			params: params
		});
		return response.data;
	}
};

export default yezi;